package com.study.result;

import com.study.enums.HttpStatusEnum;

import java.util.HashMap;

/**
 * 标准返回格式
 */
public class AjaxResult extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;

    /** 状态码 */
    public static final String CODE_TAG = "code";

    /** 返回内容 */
    public static final String MSG_TAG = "msg";

    /** 数据对象 */
    public static final String DATA_TAG = "data";
    

    public AjaxResult(int code, String msg) {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
    }

    public AjaxResult(int code, String msg, Object data) {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
        if (data != null) {
            super.put(DATA_TAG, data);
        }
    }


    /**
     * 返回成功消息
     * @return 成功消息
     */
    public static AjaxResult success() {
        return AjaxResult.success(HttpStatusEnum.SUCCESS.getMsg());
    }

    public static AjaxResult success(String msg) {
        return AjaxResult.success(msg, null);
    }

    public static AjaxResult success(Object data) {
        return AjaxResult.success(HttpStatusEnum.SUCCESS.getMsg(), data);
    }

    public static AjaxResult success(String msg, Object data) {
        return new AjaxResult(HttpStatusEnum.SUCCESS.getCode(), msg, data);
    }


    /**
     * 返回错误消息
     * @return 错误消息
     */
    public static AjaxResult fail() {
        return AjaxResult.fail(HttpStatusEnum.ERROR.getMsg());
    }

    public static AjaxResult fail(String msg) {
        return AjaxResult.fail(msg, null);
    }

    public static AjaxResult fail(String msg, Object data) {
        return new AjaxResult(HttpStatusEnum.ERROR.getCode(), msg, data);
    }

    public static AjaxResult fail(int code, String msg) {
        return new AjaxResult(code, msg);
    }


    /**
     * 响应返回结果
     *
     * @param rows 影响行数
     * @return 操作结果
     */
    public static AjaxResult toAjax(int rows) {
        return rows > 0 ? AjaxResult.success() : AjaxResult.fail();
    }

}

